package mrdemo009;

import java.io.IOException;
import java.util.Arrays;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class FlowReduce extends Reducer<Text, FlowBean, Text, FlowBean> {

	@Override
	protected void reduce(Text k2, Iterable<FlowBean> v2s,
			Reducer<Text, FlowBean, Text, FlowBean>.Context context) throws IOException, InterruptedException {
		
		long sum_upFlow = 0;
		long sum_downFlow = 0;

		// 1 遍历所用bean，将其中的上行流量，下行流量分别累加
		for (FlowBean flowBean : v2s) {
			sum_upFlow += flowBean.getSumFlow();
			sum_downFlow += flowBean.getDownFlow();
		}

		// 2 封装对象
		FlowBean resultBean = new FlowBean(sum_upFlow, sum_downFlow);
		
		// 3 写出
		context.write(k2, resultBean);
	}

}
